package com.neusoft.neuiotms.dcam.fixed.converter;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.InventoryResultTemporaryDTO;
import com.neusoft.neuiotms.dcam.fixed.model.InventoryResultTemporary;
import com.neusoft.neuiotms.dcam.fixed.model.enums.InventoryDutyResultStatus;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetTypeRepository;

@Component
public class InventoryResultTemporaryConverter
        extends AbstractConverter<InventoryResultTemporary, InventoryResultTemporaryDTO> {

    @Autowired
    private AssetTypeRepository assetTypeRepository;

    @Override
    public InventoryResultTemporary toModel(final InventoryResultTemporaryDTO dto) {
        InventoryResultTemporary model;
        //        if (dto.isNew()) {
        model = new InventoryResultTemporary();
        model.setName(dto.getName());
        model.setCode(dto.getCode());
        model.setSerialnumber(dto.getSerialnumber());
        model.setType(this.assetTypeRepository.findByCode(dto.getType()));
        model.setContractNo(dto.getContractNo());
        model.setLocationCode(dto.getLocationCode());
        model.setLocationName(dto.getLocationName());
        model.setStatus(dto.getStatus());
        model.setUserId(dto.getUserId());
        model.setUserName(dto.getUserName());
        model.setOrgId(dto.getOrgId());
        model.setOrgName(dto.getOrgName());
        model.setInventoryDutyId(dto.getInventoryDutyId());
        //        } else {
        // 更新时，只能改位置、部门、使用人
        // 查询原始数据
        //            model = this.inventoryDutyResultRepository.getOne(dto.getId());
        // 变更位置
        model.setUpdateLocationCode(dto.getUpdateLocationCode());
        model.setUpdateLocationName(dto.getUpdateLocationName());
        // 变更部门
        model.setUpdateOrgCode(dto.getUpdateOrgCode());
        model.setUpdateOrgName(dto.getUpdateOrgName());
        // 变更使用人
        model.setUpdateUserId(dto.getUpdateUserId());
        model.setUpdateUserName(dto.getUpdateUserName());
        //        }
        model.setAssetId(dto.getAssetId());
        // 盘点结果
        model.setDutyResultStatus(dto.getDutyResultStatus());
        model.setNote(dto.getNote());
        model.setUpdateTime(new Date());

        return model;
    }

    @Override
    public InventoryResultTemporaryDTO toDTO(final InventoryResultTemporary model, final boolean forListView) {
        // TODO Auto-generated method stub
        return null;
    }

    public InventoryResultTemporaryDTO toDTO(final AssetDTO source, final Long dutyId) {
        final InventoryResultTemporaryDTO dto = new InventoryResultTemporaryDTO();
        dto.setCode(source.getCode());
        dto.setName(source.getName());
        dto.setSerialnumber(source.getSerialnumber());
        dto.setType(source.getSmallTypeCode());
        dto.setContractNo(source.getContractNo());
        dto.setLocationCode(source.getLocation());
        dto.setLocationName(source.getLocationName());
        dto.setUserId(source.getManagementUserCode());
        dto.setUserName(source.getManagementUserName());
        dto.setOrgId(source.getManagementOrgCode());
        dto.setOrgName(source.getManagementOrgName());
        // 盘点状态初始化为待盘点
        dto.setDutyResultStatus(InventoryDutyResultStatus.wait);
        dto.setInventoryDutyId(dutyId);
        dto.setAssetId(source.getId());
        return dto;
    }

    public InventoryResultTemporaryDTO toDTO(final AssetDTO source, final Long dutyId, final String createPerson) {
        final InventoryResultTemporaryDTO dto = new InventoryResultTemporaryDTO();
        dto.setCode(source.getCode());
        dto.setName(source.getName());
        dto.setSerialnumber(source.getSerialnumber());
        dto.setType(source.getSmallTypeCode());
        dto.setContractNo(source.getContractNo());
        dto.setLocationCode(source.getLocation());
        dto.setLocationName(source.getLocationName());
        dto.setUserId(createPerson);
        dto.setUserName(source.getManagementUserName());
        dto.setOrgId(source.getManagementOrgCode());
        dto.setOrgName(source.getManagementOrgName());
        // 盘点状态初始化为待盘点
        dto.setDutyResultStatus(InventoryDutyResultStatus.wait);
        dto.setDutyId(dutyId);
        dto.setInventoryDutyId(dutyId);
        dto.setAssetId(source.getId());
        return dto;
    }

}
